package logic

import (
	"context"
	"errors"
	"goods-master/model"

	"goods-master/goodsrpc/goodsrpc"
	"goods-master/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品添加
func (l *GoodsAddLogic) GoodsAdd(in *goodsrpc.GoodsAddRequest) (*goodsrpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	err := user.FindUserById(model.DB, in.UserId)
	if err != nil {
		return nil, errors.New("请登录")
	}
	goods := model.Goods{
		GoodsName:  in.GoodsName,
		GoodsPrice: in.GoodsPrice,
		UserId:     int64(user.ID),
	}
	err = goods.GoodsAdd(model.DB)
	if err != nil {
		return nil, errors.New("商品添加失败")
	}
	return &goodsrpc.GoodsAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
